﻿using System;
using System.Linq;
using System.Web.Mvc;
using BLL;
using Model;


namespace Web.Areas.Manager.Controllers
{
    [UserAuthorize]
    public class LeaveApplicationVerifyController : Controller
    {
        LeaveApplication_BLL _LeaveApplication_BLL = new LeaveApplication_BLL();
        LeaveApplicationlog_BLL _LeaveApplicationlog_BLL = new LeaveApplicationlog_BLL();
        LeaveApplicationType_BLL _LeaveApplicationType_BLL = new LeaveApplicationType_BLL();
        VerifyState_BLL _VerifyState_BLL = new VerifyState_BLL();

        public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _LeaveApplication_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.Sys_Users.UserName.Equals(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }

        public virtual ActionResult Details(Guid Id)
        {
            ViewBag.VerifyState = _VerifyState_BLL.Get();
            ViewBag.LeaveApplicationType = _LeaveApplicationType_BLL.Get();
            var item = _LeaveApplication_BLL.Get(Id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid Id)
        {
            //状态下拉列表
            ViewBag.VerifyState = _VerifyState_BLL.Get();
            ViewBag.LeaveApplicationType = _LeaveApplicationType_BLL.Get();
            var item = _LeaveApplication_BLL.Get(Id);
            return View(item);
        }

        [HttpPost]
        public virtual ActionResult Edit(Guid Id, LeaveApplicationLog collection)
        {
            var item = _LeaveApplication_BLL.Get(Id);
            ViewBag.VerifyState = _VerifyState_BLL.Get();

            if (!ModelState.IsValid)
            {
                ViewBag.LeaveApplicationType = _LeaveApplicationType_BLL.Get();
                ViewBag.LeaveApplicationType = _LeaveApplicationType_BLL.Get();
                return View(item);
            }

            Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
            var UserId = _Sys_Users_BLL.Get(User.Identity.Name).Id;

            collection.Id = Guid.NewGuid();
            collection.LeaveApplicationId = item.Id;
            collection.UserId = UserId;
            collection.AddDate = DateTime.Now;

            item.StateId = collection.StateId;
            item.LeaveApplicationLogs.Add(collection);

            _LeaveApplication_BLL.Save();

            return RedirectToAction("Index", Helper.MyrouteValues.Get());
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
            _LeaveApplication_BLL.Delete(Id);
            return RedirectToAction("Index", Helper.MyrouteValues.Get());
        }
    }
}
